package com.example.myapplication.tack_33;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;

import org.json.JSONException;
import org.json.JSONObject;

public class CommentActivity extends AppCompatActivity {
    private ImageView exit;
    private TextView submit;
    private EditText edit;
    private RatingBar ratingBar;
    private JSONObject jsonObject;
    private Intent intent;
    private String stringExtra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_33_activity_comment);
        init();
        initExit();
        initSubmit();
    }

    private void initSubmit() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //"content": "真好吃，下次还来",
                //"orderNo": "2021051120444612594",
                //"score": 5
                String content = edit.getText().toString();
                int rating = (int)ratingBar.getRating();
                if(!TextUtils.isEmpty(content)){
                    try {
                        jsonObject.put("content",content);
                        jsonObject.put("orderNo",stringExtra);
                        jsonObject.put("score",String.valueOf(rating));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String json = jsonObject.toString();
                    comment(json);
                }else{
                    Toast.makeText(CommentActivity.this,"请填写你的评论",Toast.LENGTH_SHORT).show();

                }

            }
        });

    }

    private void comment(String json) {
        String url = "prod-api/api/takeout/comment";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(CommentActivity.this,"评论成功",Toast.LENGTH_SHORT).show();
                            CommentActivity.this.finish();
                        }else{
                            Toast.makeText(CommentActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        submit = findViewById(R.id.submit);
        edit = findViewById(R.id.edit);
        ratingBar = findViewById(R.id.ratingBar);
        intent = getIntent();
        stringExtra = intent.getStringExtra(Common.COMMON);
        jsonObject = new JSONObject();
    }
}